﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NUS.ISS.ARS.BusinessFacade.Interface;
using NUS.ISS.ARS.BusinessFacade;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.BusinessEntity.ViewModels;


namespace NUS.ISS.ARS.Web.Admin.Controllers
{

    
    public class FlightScheduleController : Controller
    {
       
        // GET: /FlightSchedule/
        [HttpGet]
        public ActionResult FlightScheduleCreate()
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            FlightScheduleView flightscheduleview = new FlightScheduleView();
            flightscheduleview.ListofAircraftname = a.IEnumGetAllAircraft();
            flightscheduleview.ListofAirportname = a.IEnumGetAllAirport();
            IList<SelectListItem> listofairplane = GetDropdownlistItems(flightscheduleview, "airplane");
            ViewData["airplaneid"] = listofairplane;
            IList<SelectListItem> listoffromairport = GetDropdownlistItems(flightscheduleview, "fromairport");
            IList<SelectListItem> listoftoairport = GetDropdownlistItems(flightscheduleview, "toairport");
            ViewData["fromairportid"] = listoffromairport;
            ViewData["toairportid"] = listoftoairport;
            return View(flightscheduleview);

        }

        [HttpPost]
        public ActionResult FlightScheduleCreate(string airplaneid, string fromairportid, string toairportid, FlightScheduleView flightscheduleview)
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            flightscheduleview.ListofAircraftname = a.IEnumGetAllAircraft();
            flightscheduleview.ListofAirportname = a.IEnumGetAllAirport();
            IList<SelectListItem> listofairplane = GetDropdownlistItems(flightscheduleview, "airplane");
            ViewData["airplaneid"] = new SelectList(listofairplane, "Value", "Text", airplaneid);
            ViewData["airplaneid"] = listofairplane;
            IList<SelectListItem> listoffromairport = GetDropdownlistItems(flightscheduleview, "fromairport");
            IList<SelectListItem> listoftoairport = GetDropdownlistItems(flightscheduleview, "toairport");
            ViewData["fromairportid"] = new SelectList(listoffromairport, "Value", "Text", fromairportid);
            ViewData["fromairportid"] = listoffromairport;
            ViewData["toairportid"] = new SelectList(listoftoairport, "Value", "Text", toairportid);
            ViewData["toairportid"] = listoftoairport;
            flightscheduleview.ToAirportId = Convert.ToInt32(toairportid);
            flightscheduleview.FromAirportId = Convert.ToInt32(fromairportid);
            flightscheduleview.AirplaneId = Convert.ToInt32(airplaneid);
            if (toairportid != fromairportid)
            {
                a.SaveSchedule(flightscheduleview);
                ViewData["Error1"] = "Data had been Updated";
            }
            else
            {
                ViewData["Error2"] = "Both of the Airport names are the same.";
            }
            return View(flightscheduleview);

        }





        [HttpGet]
        public ActionResult FlightScheduleEdit(long id)
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            FlightScheduleView flightscheduleview;
           // FlightSchedule flightschedule=null;
            flightscheduleview = a.GetFlightSchedulesById(id);
            flightscheduleview.ListofAircraftname=a.IEnumGetAllAircraft();
            flightscheduleview.ListofAirportname = a.IEnumGetAllAirport();
            IList<SelectListItem> listofairplane = GetDropdownlistItems(flightscheduleview,"airplane");
            ViewData["airplaneid"] = listofairplane;
            IList<SelectListItem> listoffromairport = GetDropdownlistItems(flightscheduleview, "fromairport");
            IList<SelectListItem> listoftoairport = GetDropdownlistItems(flightscheduleview, "toairport");
            ViewData["fromairportid"] = listoffromairport;
            ViewData["toairportid"] = listoftoairport;
            return View(flightscheduleview);
        }
        public IList<SelectListItem> GetDropdownlistItems(FlightScheduleView flightscheduleview,string parameter)
        {
            IList<SelectListItem> ilSelectList = new List<SelectListItem>();
            if (parameter.CompareTo("airplane") == 0)
            {
                foreach (var aircraft in flightscheduleview.ListofAircraftname)
                {
                    SelectListItem selectListItem = new SelectListItem();
                    selectListItem.Text = aircraft.Model;
                    selectListItem.Value = aircraft.AircraftId.ToString();
                    if (aircraft.AircraftId == flightscheduleview.AirplaneId) selectListItem.Selected = true;
                    else selectListItem.Selected=false;
                    ilSelectList.Add(selectListItem);
                }
            }
            else if (parameter.CompareTo("fromairport") == 0)
            {
                foreach (var airport in flightscheduleview.ListofAirportname)
                {
                    SelectListItem selectListItem = new SelectListItem();
                    selectListItem.Text = airport.AirportName;
                    selectListItem.Value = airport.AirportId.ToString();
                    if (airport.AirportId == flightscheduleview.FromAirportId) selectListItem.Selected = true;
                    else selectListItem.Selected = false;
                    ilSelectList.Add(selectListItem);
                }
            }
            else if (parameter.CompareTo("toairport") == 0)
            {
                foreach (var airport in flightscheduleview.ListofAirportname)
                {
                    SelectListItem selectListItem = new SelectListItem();
                    selectListItem.Text = airport.AirportName;
                    selectListItem.Value = airport.AirportId.ToString();
                    if (airport.AirportId == flightscheduleview.ToAirportId) selectListItem.Selected = true;
                    else selectListItem.Selected = false;
                    ilSelectList.Add(selectListItem);
                }
            }
            return ilSelectList;

        }

        [HttpPost]
        public ActionResult FlightScheduleEdit(string airplaneid, string fromairportid, string toairportid, FlightScheduleView flightscheduleview)
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            flightscheduleview.ListofAircraftname = a.IEnumGetAllAircraft();
            flightscheduleview.ListofAirportname = a.IEnumGetAllAirport();
            IList<SelectListItem> listofairplane = GetDropdownlistItems(flightscheduleview, "airplane");
            ViewData["airplaneid"] = new SelectList(listofairplane, "Value", "Text", airplaneid);
            ViewData["airplaneid"] = listofairplane;
            IList<SelectListItem> listoffromairport = GetDropdownlistItems(flightscheduleview, "fromairport");
            IList<SelectListItem> listoftoairport = GetDropdownlistItems(flightscheduleview, "toairport");
            ViewData["fromairportid"] = new SelectList(listoffromairport, "Value", "Text", fromairportid);
            ViewData["fromairportid"] = listoffromairport;
            ViewData["toairportid"] = new SelectList(listoftoairport, "Value", "Text", toairportid);
            ViewData["toairportid"] = listoftoairport;
            flightscheduleview.ToAirportId = Convert.ToInt32(toairportid);
            flightscheduleview.FromAirportId = Convert.ToInt32(fromairportid);
            flightscheduleview.AirplaneId = Convert.ToInt32(airplaneid);
            if (toairportid != fromairportid) 
            {
                a.SaveChangedSchedule(flightscheduleview);
                ViewData["Error3"] = "Data had been Updated";
            }
            else
            {
                ViewData["Error4"] = "Both of the Airport names are the same.";
            }
            return View(flightscheduleview);
        }

        [HttpGet]
        public ActionResult FlightScheduleDelete(long id)
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            FlightScheduleView flightscheduleview;
            // FlightSchedule flightschedule=null;
            flightscheduleview = a.GetFlightSchedulesById(id);
            return View(flightscheduleview);
        }

        [HttpPost]
        public ActionResult FlightScheduleDelete(FlightScheduleView flightscheduleview)
        {
           IAdminBF a = BFFactory.CreateAdminBFInstance();
           flightscheduleview.isDeleted = true;
           a.SaveChangedSchedule(flightscheduleview);
           return RedirectToAction("FlightSchedule");
        }

        [HttpGet]
        public ActionResult FlightSchedule()
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            IEnumerable<FlightScheduleView> flightscheduleview;
            flightscheduleview = a.IEnumGetAllFlightSchedules();
            return View(flightscheduleview);
        }
        [HttpPost]
        public ActionResult FlightSchedule(String flightnumber, String fromairportid, string toairportid)
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();

            IEnumerable<FlightScheduleView> FlightScheduleView;

            if (!((string.IsNullOrEmpty(flightnumber)) && (string.IsNullOrEmpty(toairportid)) && (string.IsNullOrEmpty(fromairportid))))
            {

                FlightScheduleView = a.GetFlightSchedulesByFlightNumberAirport(flightnumber, fromairportid, toairportid);
            }
            else
            {
                FlightScheduleView = a.IEnumGetAllFlightSchedules();

            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(FlightScheduleView);
            }
            else
            {
                return View(FlightScheduleView);
            }

            //   ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            //  return View(a.GetAllAircraft());

            //   return View(a.GetFlightSchedulesByFlightNumberAirport(flightnumber, fromairportid, toairportid));
        }

    }
}
